package com.yangchao.ch03
/** groovy正则表达式写法 **/
class example13 {
    public static void main(String[] args) {
        String targetStr = "8956";  //目标字符串
        String regex = "(\\d)(\\d)(\\d)(\\d)"; //正则表达式字符串
        /**

        def p = ~regex       //Pattern p =Pattern.complie(regex); p is Pattern java中的写法
        boolean  isMatch=p.matcher(targetStr).matches()           //完整匹配
        println("完整匹配："+isMatch)

        **/

        def p= ~regex
        boolean isMatch=(targetStr ==~ regex) //Groovy的语法，完整匹配
        println("完整匹配："+isMatch)


        //捕获组
        def m =(targetStr =~ regex)  //Pattern.compile(regex).mathcer(targetStr)
        m.find {
           boolean Matcher group ->
                println("Group": +group) //group 是一个数组
                for (int i=1;i <= group.size();i++){
                    println("Group "+i+":"+group.getAt(i));
                }
        }


    }
}
